package com.mg.flightplan.control;

import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.Session.AccessType;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.os.Environment;
import android.util.Log;
import android.view.View;

public abstract class AbstractFPlanActivity extends Activity {

	private static final String APP_KEY = "lbt63jmord68ree";
	private static final String APP_SECRET = "bhaonnyc8psgi89";
	private static final String ACCESS_KEY_NAME = "ACCESS_KEY";
	private static final String ACCESS_SECRET_NAME = "ACCESS_SECRET";
	private static final String ACCOUNT_PREFS_NAME = "prefMyFlightPlan3";
	private static final AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
	private String tag = "AbstractFPlanActivity";

	public AbstractFPlanActivity() {
		super();
	}

    protected void showTextBox(String message, OnClickListener ocl){
	    // prepare the alert box
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        // set the message to display
        alertbox.setMessage(message);

        // add a neutral button to the alert box and assign a click listener
        alertbox.setNeutralButton("Ok", ocl);

        // show it
        alertbox.show();
    }
	protected boolean checkStorageReady() {
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();
	
		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    mExternalStorageWriteable = false;
		} else {
		    // Something else is wrong. It may be one of many other states, but all we need
		    //  to know is we can neither read nor write
		    mExternalStorageWriteable = false;
		}
		
		return mExternalStorageWriteable;
	}

	/**
	 * Shows keeping the access keys returned from Trusted Authenticator in a local
	 * store, rather than storing user name & password, and re-authenticating each
	 * time (which is not to be done, ever).
	 */
	protected void storeKeys(String key, String secret) {
	    // Save the access key for later
		Log.d(tag, "Storing token pair: "+key+"/"+secret);
	    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
	    Editor edit = prefs.edit();
	    edit.putString(ACCESS_KEY_NAME, key);
	    edit.putString(ACCESS_SECRET_NAME, secret);
	    
	    edit.commit();
	}

	public void clearKeys(View view) {
	    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
	    Editor edit = prefs.edit();
	    edit.clear();
	    edit.commit();
	}

	protected AndroidAuthSession buildSession() {
	    AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
	    AndroidAuthSession session;
	
	    String[] stored = getKeys();
	    if (stored != null) {
	    	Log.d(tag, "Found the following token pair: "+stored[0]+"/"+stored[1]);
	        AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
	        session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE, accessToken);
	    } else {
	    	Log.d(tag, "Didn't find a token pair, will ask the user then...");
	        session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
	    }
	
	    return session;
	}

	/**
	 * Shows keeping the access keys returned from Trusted Authenticator in a local
	 * store, rather than storing user name & password, and re-authenticating each
	 * time (which is not to be done, ever).
	 *
	 * @return Array of [access_key, access_secret], or null if none stored
	 */
	private String[] getKeys() {
	    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
	    String key = prefs.getString(ACCESS_KEY_NAME, null);
	    String secret = prefs.getString(ACCESS_SECRET_NAME, null);
	    if (key != null && secret != null) {
	    	String[] ret = new String[2];
	    	ret[0] = key;
	    	ret[1] = secret;
	    	return ret;
	    } else {
	    	return null;
	    }
	}

}